<?php
class Goods {

	function upload($data){
		$year = date('Y',time());
		$yue = date('m',time());
		$ri = date('d',time());
		$riqi = date('md',time());
		$base64_img = iconv('UTF-8','gb2312',$data['img']);
		if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
			$filetype = $result[2];
			if($filetype=="jpeg"){
				$filetype = "jpg";
			}
			if($filetype!='jpg' && $filetype!='gif' && $filetype!='png'){ error('ERROR');}
			$rq = $riqi.time().rand(1000,9999);
			if(!file_exists("upload/$year$yue$ri")){
				$qx = umask(0);
				mkdir("upload/$year$yue$ri",0777,true);
				umask($qx);
				$new_file = "upload/$year$yue$ri/" .$rq.".".$filetype;
				if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
					$imgpath = "/upload/$year$yue$ri/" .$rq.".".$filetype;
					$response['0'] = 1;    
					$response['1'] = $imgpath;
					echo json_encode($response); 
				}else{				
					error("上传失败");
				}		
			}else{
				$new_file = "upload/$year$yue$ri/" .$rq.".".$filetype;
				if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
					$imgpath = "/upload/$year$yue$ri/" .$rq.".".$filetype;
					$response['0'] = 1;    
					$response['1'] = $imgpath;
					echo json_encode($response); 
				}else{				
					error("上传失败");
				}
			}	
		}else{
			error("上传失败");
		}
	}

	function getjllist($data){
		global $conf;
		global $jjarr;
		$db = new db($conf);
		$oid = isset($data['id'])?intval($data['id']):0;
		$list = $db->field(['c.*','m.uname','m.tname'])->where("oid = $oid")->order('Id desc')->select("sc_cash c left join sc_member m on m.Id=c.uid");
		$str = '';
		if($list){
			$str .= '<table><tr><th>ID</th><th>账号</th><th>姓名</th><th>类型</th><th>奖励</th><th>时间</th></tr>';
			for($i=0;$i<count($list);$i++){
				$str .= "<tr><td>".$list[$i]['Id']."</td><td>".$list[$i]['uname']."</td><td>".$list[$i]['tname']."</td><td>".$jjarr[$list[$i]['type']]."</td><td>".$list[$i]['cash']."</td><td>".$list[$i]['addtime']."</td></tr>";
			}
			$str .= "</table>";
			success($str);
		}		
		error('未查询到奖励记录');
	}

	function gindex($data){
        global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id")->select("sc_goods");
		if(!$info){ error('通讯异常');}
		if($info[0]['index1']==1){
		    $db->where("Id=$id")->update("sc_goods",['index1'=>0]);
		    $str = [1,2,3,'否'];
		}else{
		    $db->where("Id=$id")->update("sc_goods",['index1'=>1]);
		    $str = [1,3,2,'是'];
		}
		echo json_encode($str);
    }
    
    function gstate($data){
        global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id")->select("sc_goods");
		if(!$info){ error('通讯异常');}
		if($info[0]['gstate']==1){
		    $db->where("Id=$id")->update("sc_goods",['gstate'=>0]);
		    $str = [1,2,3,'已下架'];
		}else{
		    $db->where("Id=$id")->update("sc_goods",['gstate'=>1]);
		    $str = [1,3,2,'上架中'];
		}
		echo json_encode($str);
    }

	function jhend($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id and cplx<3 and jhstate=1 and state>0 and state<5")->select("sc_orders");
		if(!$info){ error("未查询到订单信息");}
		$up = $db->where("Id=$id")->update("sc_orders",['jhstate'=>2,'jhendtime'=>date("Y-m-d H:i:s")]);
		if($up){
			success('操作成功');
		}else{
			error('确认失败');
		}
	}

	function consoleorder($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id and state>0 and state<5 and td=0")->select("sc_orders");
		if(!$info){ error("未查询到订单信息");}
		$up = $db->where("Id=$id")->update("sc_orders",['td'=>1,'tdtime'=>date("Y-m-d H:i:s")]);
		if($up){			
			success('退单成功');
		}else{
			error('退单失败');
		}
	}

	function orderqr($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id and cplx=2 and state=1")->select("sc_orders");
		if(!$info){ error("未查询到订单信息");}
		$up = $db->where("Id=$id")->update("sc_orders",['state'=>2,'qrtime'=>date("Y-m-d H:i:s")]);
		if($up){
			success('操作成功');
		}else{
			error('确认失败');
		}
	}

	public function zt($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id")->select("sc_orders");
		if(!$info){ error("未查询到订单信息");}
		if($info[0]['state']==0){ error("未支付订单");}
		if($info[0]['state']>2){ error("已处理订单 请勿重复操作");}
		$db->where("Id=$id")->update("sc_orders",array('state'=>4,"endtime"=>date("Y-m-d H:i:s",time()),"zt"=>1));
		success("操作成功");
	}

	public function orderdel($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"通信异常，请刷新");
			echo json_encode($str);exit;
		}
		$id = isset($data["id"])?intval($data["id"]):0;
		if(!$id){
			$str = array(0,"ERROR");
			echo json_encode($str);exit;
		}
		$field = array("state"=>6);
		$result = $db->where(array("Id"=>$id))->update("sc_orders",$field);
		if(!$result){
			$str = array(0,"信息异常");
			echo json_encode($str);exit;
		}else{
			
			$str = array(1,"删除成功");
			echo json_encode($str);exit;
		}
	}

	public function orderedit($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"通信异常，请刷新");
			echo json_encode($str);exit;
		}
		$id = isset($data["oid"])?intval($data["oid"]):0;
		$info = $db->field(array("*"))->where("Id=$id")->select("sc_orders");
		if($info){
			$trackname = isset($data["tckname"])?$data["tckname"]:"";
			$trackcode = isset($data["tckcode"])?$data["tckcode"]:"";
			$field = array("sendtime"=>date("Y-m-d H:i:s",time()),"trackname"=>$trackname,"trackcode"=>$trackcode);
			$cplx = $info[0]['cplx'];
		
			$field['state'] = 3;
			$up = $db->where(array("Id"=>$id,"state"=>2))->update("sc_orders",$field);
				
			if(!$up){
				error('操作失败');
			}else{				
				success("操作成功");
			}			
		}		
	}

	function cat($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if(!$id || $id!=$data['id']){ error("类型错误");}
		$r1 = $db->field(array("*"))->where("leixing=$id")->select("sc_catogry");
		if($r1){
			$str = "";
			for($i=0;$i<count($r1);$i++){
				$str = $str."<option value='".$r1[$i]['Id']."'>".$r1[$i]['name']."</option>";
			}
			success($str);
		}
		error("暂无分类");
	}
	//添加商品分类
    public function tagadd($data){
		global $conf;
		$db = new db($conf);		
		if(count($data)<=0 || htmlspecialchars($data['t0'])==""){ error("请输入分类名称");}
		$name = isset($data['t0'])?htmlspecialchars($data['t0']):"";
		$state = isset($data['t2'])?intval($data['t2']):0;
		$px = isset($data['t3'])?intval($data['t3']):0;
		$level=isset($data['t4'])?intval($data['t4']):0;
		if($level<1 || $level>3){ error("属性类型错误");}
		if(empty($name)){  error("请输入分类名称");}
		
		$field = array("name"=>$name,"state"=>$state,"px"=>$px,"level"=>$level);
		$result = $db->insert("sc_tag",$field);
		$str = $result?array(1,"添加成功"):array(0,"添加失败");
		echo json_encode($str);
	}
	
	//编辑商品分类
	public function tagedit($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0 || htmlspecialchars($data['t0'])==""){
			 error("请输入分类名称");
		}
		$id = isset($data["id"])?intval($data["id"]):0;
		if(empty($id)){  error("信息异常，请刷新页面");}
		$name = isset($data['t0'])?htmlspecialchars($data['t0']):"";
		$state = isset($data['t2'])?$data['t2']:0;
		$px = isset($data['t3'])?intval($data['t3']):0;
		$level=isset($data['t4'])?intval($data['t4']):1;
		if($level<1 || $level>3){ error("产品类型错误");}
		if(empty($name)){  error("请输入分类名称");}		
		
		$field = array("name"=>$name,"state"=>$state,"px"=>$px);
		$result = $db->where(array("Id"=>$id))->update("sc_tag",$field);
		$str = $result?array(1,"修改成功"):array(0,"修改失败");
		echo json_encode($str);
    }
	//添加商品分类
    public function cateadd($data){
		global $conf;
		$db = new db($conf);		
		if(count($data)<=0 || htmlspecialchars($data['t0'])==""){ error("请输入栏目名称");}
		$name = isset($data['t0'])?htmlspecialchars($data['t0']):"";
		$dirname = isset($data['t1'])?htmlspecialchars($data['t1']):"";
		$ismenu = isset($data['t2'])?intval($data['t2']):0;
		$px = isset($data['t3'])?intval($data['t3']):0;
		$pid = isset($data['pid'])?intval($data['pid']):0;		
		$leixing=isset($data['t4'])?intval($data['t4']):0;
		if($leixing<1 || $leixing>3){ error("产品类型错误");}
		if(empty($name)){  error("请输入栏目名称");}
		$level=1;		
		$field = array("name"=>$name,"dirname"=>$dirname,"ismenu"=>$ismenu,"leixing"=>$leixing,"px"=>$px,"level"=>$level);
		$result = $db->insert("sc_catogry",$field);
		$str = $result?array(1,"添加成功"):array(0,"添加失败");
		echo json_encode($str);
	}
	
	//编辑商品分类
	public function cateedit($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0 || htmlspecialchars($data['t0'])==""){
			 error("请输入栏目名称");
		}
		$id = isset($data["id"])?intval($data["id"]):0;
		if(empty($id)){  error("信息异常，请刷新页面");}
		$name = isset($data['t0'])?htmlspecialchars($data['t0']):"";
		$dirname = isset($data['t1'])?htmlspecialchars($data['t1']):"";
		$ismenu = isset($data['t2'])?$data['t2']:0;
		$px = isset($data['t3'])?intval($data['t3']):0;
		$pid = isset($data['pid'])?intval($data['pid']):0;
		$leixing=isset($data['t4'])?intval($data['t4']):1;
		if($leixing<1 || $leixing>3){ error("产品类型错误");}
		if(empty($name)){  error("请输入栏目名称");}		
		
		$field = array("name"=>$name,"dirname"=>$dirname,"ismenu"=>$ismenu,"px"=>$px);
		$result = $db->where(array("Id"=>$id))->update("sc_catogry",$field);
		$str = $result?array(1,"修改成功"):array(0,"修改失败");
		echo json_encode($str);
    }

	
//添加商品
public function goodadd($data){
	global $conf;
	$db = new db($conf);
	if(count($data)<=0){
		$str = array(0,"信息已更新，请刷新页面");
		echo json_encode($str);exit;
	}
	$cid 		= isset($data['cat1'])?intval($data['cat1']):0;
	$gname 		= isset($data['t0'])?htmlspecialchars($data['t0']):"";
	$gthumb 	= isset($data['img'])?$data['img']:"";
	$yprice 	= isset($data['yprice'])?(float)$data['yprice']:0;	
	$cash 		= isset($data['cash'])?(float)$data['cash']:0;	
	$ztcash 	= isset($data['ztcash'])?(float)$data['ztcash']:0;	
	$jdcash 	= isset($data['jdcash'])?(float)$data['jdcash']:0;
	$type 		= isset($data['type'])?intval($data['type']):0;
	$tag1 		= isset($data['tag1'])?intval($data['tag1']):0;
	$tag2 		= isset($data['tag2'])?intval($data['tag2']):0;
	$kuaidif 	= isset($data['kdf'])?(float)$data['kdf']:0;
	$gtotal 	= isset($data['t9'])?intval($data['t9']):0;
	$selltotal 	= isset($data['t10'])?intval($data['t10']):0;
	$shuxing 	= isset($data['t11'])?htmlspecialchars($data['t11']):"";
	$px 		= isset($data['t12'])?intval($data['t12']):0;
	$ticket 	= isset($data['ticket'])?htmlspecialchars($data['ticket']):"";
	$gcontent 	= isset($data['editorValue'])?htmlspecialchars($data['editorValue']):0;
	$addtime 	= date("Y-m-d H:i:s",time());
	
	if($cid<=0){ error("请选择商品分类");}
	if(strlen($gname)<=2){ error("请输入商品名称");}
	if(count($gthumb)==0){ error("请上传产品缩略图");}
	if(empty($gcontent)){ error("请输入商品详情");}	
	$gthumb1 = json_encode($gthumb);
	$gthumb = $gthumb[0];
	//获取分类信息
	$c1 = $db->field(array("*"))->where("Id=$cid")->select("sc_catogry");
	if(!$c1){ error("请选择商品分类");}
	
	$cplx = $c1[0]['leixing'];
	
	if($cplx!=2 && $cash<=0){ error("请填写产品价格");}	
	$sku_json = isset($data['data'])?$data['data']:"";
	$sku_arr = json_decode($sku_json,true);
	$sku = array();
	if($sku_arr){
		if((!$sku_arr['color'] && $sku_arr['sizes']) || (!$sku_arr['sizes'] && $sku_arr['color'])){ error("请完善规格信息");}
		for($i=0;$i<count($sku_arr['color']);$i++){
			for($j=0;$j<count($sku_arr['sizes']);$j++){
				$num = $i*count($sku_arr['sizes']) + $j;
				if(!$sku_arr['kucun'][$num]){
					$sku_arr['kucun'][$num] = 0;
				}
				if(!$sku_arr['xiaoliang'][$num]){
					$sku_arr['xiaoliang'][$num] = 0;
				}
				$sku[$num] = array($sku_arr['color'][$i],$sku_arr['sizes'][$j],$sku_arr['kucun'][$num],$sku_arr['xiaoliang'][$num]);
			}
		}
	}
	
	$field = array("cid"=>$cid,"gname"=>$gname,"gthumb"=>$gthumb,"gthumb1"=>$gthumb1,"yprice"=>$yprice,"cash"=>$cash,"ztcash"=>$ztcash,"jdcash"=>$jdcash,"type"=>$type,"tag1"=>$tag1,"tag2"=>$tag2,"kuaidif"=>$kuaidif,"gtotal"=>$gtotal,"selltotal"=>$selltotal,"shuxing"=>$shuxing,"px"=>$px,"gcontent"=>$gcontent,"addtime"=>$addtime,"ticket"=>$ticket,"cplx"=>$cplx);		
	
	$result = $db->insert("sc_goods",$field);
	$str = $result?array(1,"添加成功"):array(0,"添加失败");	
	echo json_encode($str);
}
	//编辑商品//
	public function goodedit($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}

		$gid 		= isset($data['gid'])?intval($data['gid']):0;
		$cid 		= isset($data['cat1'])?intval($data['cat1']):0;
		$gname 		= isset($data['t0'])?htmlspecialchars($data['t0']):"";
		$gthumb 	= isset($data['img'])?$data['img']:"";
		$yprice 	= isset($data['yprice'])?(float)$data['yprice']:0;
		$cash 		= isset($data['cash'])?(float)$data['cash']:0;		
		$ztcash 	= isset($data['ztcash'])?(float)$data['ztcash']:0;	
		$jdcash 	= isset($data['jdcash'])?(float)$data['jdcash']:0;
		$type 		= isset($data['type'])?intval($data['type']):0;
		$tag1 		= isset($data['tag1'])?intval($data['tag1']):0;
		$tag2 		= isset($data['tag2'])?intval($data['tag2']):0;
		$kuaidif 	= isset($data['kdf'])?(float)$data['kdf']:0;
		$gtotal 	= isset($data['t9'])?intval($data['t9']):0;
		$selltotal 	= isset($data['t10'])?intval($data['t10']):0;
		$shuxing 	= isset($data['t11'])?htmlspecialchars($data['t11']):"";
		$px 		= isset($data['t12'])?intval($data['t12']):0;
		$ticket 	= isset($data['ticket'])?htmlspecialchars($data['ticket']):"";
		$gcontent 	= isset($data['editorValue'])?htmlspecialchars($data['editorValue']):0;
		$edittime 	= date("Y-m-d H:i:s",time());
		
		if($cid<=0){ error("请选择商品分类");}
		if(strlen($gname)<=2){ error("请输入商品名称");}
		if(count($gthumb)==0){ error("请上传产品缩略图");}
		if(empty($gcontent)){ error("请输入商品详情");}
		$gthumb1 = json_encode($gthumb);
		$gthumb = $gthumb[0];

		$c1 = $db->field(array("*"))->where("Id=$cid")->select("sc_catogry");
		if(!$c1){ error("请选择商品分类");}
		$ginfo = getginfo($gid);
		if(!$ginfo){ error('信息读取错误');}
		if($ginfo['cplx']!=$c1[0]['leixing']){ error("产品类型无法更改");}		
		$cplx = $c1[0]['leixing'];
		
		if($cplx!=2 && $cash<=0){ error("请填写产品价格");}

		$sku_json = isset($data['data'])?$data['data']:"";
		$sku_arr = json_decode($sku_json,true);
		
		$sku = array();
		if($sku_arr){			
			if((!$sku_arr['color'] && $sku_arr['sizes']) || (!$sku_arr['sizes'] && $sku_arr['color'])){ error("请完善规格信息");}
			for($i=0;$i<count($sku_arr['color']);$i++){
				for($j=0;$j<count($sku_arr['sizes']);$j++){
					$num = $i*count($sku_arr['sizes']) + $j;
					if(!$sku_arr['kucun'][$num]){
						$sku_arr['kucun'][$num] = 0;
					}
					if(!$sku_arr['xiaoliang'][$num]){
						$sku_arr['xiaoliang'][$num] = 0;
					}
					$sku[$num] = array($sku_arr['color'][$i],$sku_arr['sizes'][$j],$sku_arr['kucun'][$num],$sku_arr['xiaoliang'][$num]);
				}
			}
		}

		$field = array("cid"=>$cid,"gname"=>$gname,"gthumb"=>$gthumb,"gthumb1"=>$gthumb1,"yprice"=>$yprice,"cash"=>$cash,"ztcash"=>$ztcash,"jdcash"=>$jdcash,"type"=>$type,"tag1"=>$tag1,"tag2"=>$tag2,"kuaidif"=>$kuaidif,"gtotal"=>$gtotal,"selltotal"=>$selltotal,"shuxing"=>$shuxing,"px"=>$px,"gcontent"=>$gcontent,"edittime"=>$edittime,"ticket"=>$ticket);	
				
		$result = $db->where(array("Id"=>$gid))->update("sc_goods",$field);
		$str = $result?array(1,"产品更新成功"):array(0,"产品更新失败");		
		echo json_encode($str);
	}
	
	public function gooddel($data){
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}
		$id = isset($data['id'])?$data['id']:0;
		if(empty($id)){
			$str = array(0,"信息异常，请刷新当前页面");echo json_encode($str);exit;
		}
		$result = $db->where(array("Id"=>$id))->delete("sc_goods");
		$str = $result?array(1,"产品删除成功"):array(0,"产品删除失败");
		echo json_encode($str);		
	}
	
}